package com.bw.educate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.bw.educate.common.ResponseResult;
import com.bw.educate.common.auth.UserInfo;
import com.bw.educate.common.order.ReqEduOrder;
import com.bw.educate.entity.EduCourseBase;
import com.bw.educate.entity.EduOrder;
import com.bw.educate.service.IEduOrderService;
import com.bw.educate.utils.UserUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-13
 */
@RestController
@RequestMapping("eduOrder")
public class EduOrderController {


    @Autowired
    private IEduOrderService eduOrderService;

    @Autowired
    private UserUtil userUtil;

    /**
     * @Description:生成订单
     * @Author: 周明丽
     * @Date: 2020/8/25 22:12
     * @param courseId:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("addOrder")
    public ResponseResult addOrder(@RequestBody EduCourseBase courseId, HttpServletRequest request){
        ResponseResult result=ResponseResult.SUCCESS();
        UserInfo loginUser = userUtil.getLoginUser(request);
        try {
            eduOrderService.addOrder(loginUser.getId(),courseId);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("订单生成失败"+e.toString());
        }
        return result;
    }


    /**
     * @Description:根据学员id查询学员订单
     * @Author: 周明丽
     * @Date: 2020/8/26 9:20
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("list")
    public ResponseResult getByOrderId(HttpServletRequest request){
        ResponseResult result=ResponseResult.SUCCESS();
        UserInfo loginUser = userUtil.getLoginUser(request);
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("suid",loginUser.getId());
            result.setResult(eduOrderService.list(queryWrapper));
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("订单生成失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:B端订单列表页面
     * @Author: 周明丽
     * @Date: 2020/8/26 9:26
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("list/{currentPage}/{pageSize}")
    public ResponseResult list(@PathVariable("currentPage") Integer currentPage,
                               @PathVariable("pageSize") Integer pageSize,
                               @RequestBody EduOrder order){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            //开启分页
            PageHelper.startPage(pageSize,currentPage);
            List<ReqEduOrder> list = eduOrderService.listOrder();
            PageInfo pageInfo = new PageInfo(list);
            result.setResult(pageInfo);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("订单生成失败"+e.toString());
            e.printStackTrace();
        }
        return result;
    }


    /**
     * @Description:根据id删除订单
     * @Author: 周明丽
     * @Date: 2020/8/26 11:30
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("delOrder/{id}")
    public ResponseResult delOrder(@PathVariable("id") Long id){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            eduOrderService.removeById(id);
            result.setMessage("删除成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("订单删除失败"+e.toString());
        }
        return result;
    }

}
